<?php

/**
 * 公共操作
 * @author wyb
 */

namespace Logistics\Controller;
class IndexController extends BaseController {

    public function index() {

        $this->display();
    }
	
	public function setting() {
		$this->display();
	}
	
	public function info() {
		$data = $this->_post;
		if( $data ) {
			$data['province_name'] = $data['province_id'] ? M("area")->where(array("id" => $data['province_id']))->getField("name") : '';
			$data['city_name'] = $data['city_id'] ? M("area")->where(array("id" => $data['city_id']))->getField("name") : '';
			$data['area_name'] = $data['area_id'] ? M("area")->where(array("id" => $data['area_id']))->getField("name") : '';
			$data['brief'] = $data['brief'];
			if( M("logistics_supply")->where(array("id" => $this->_member_info['id']))->save( $data ) !== false ) {
				session( "login_info" , M("logistics_supply")->where(array("id" => $this->_member_info['id']))->find() ); //存入session
				$this->success("资料更新成功");
			} else {
				$this->error("信息修改失败");
			}
			
		}
		$this->display();
	}
	
	public function account() {
		$total_num = M("logistics_supply_account_log")->where(array("logistics_supply_id" => $this->_member_info['id']))->count();
		$page = intval( $this->_get['page'] ) > 1 ? $this->_get['page'] : 1;
		$data = M("logistics_supply_account_log")->where(array("logistics_supply_id" => $this->_member_info['id']))->page( $page )->limit($this->_limit)->select();
		$this->assign("account_data" , $data);
		$this->assign("page" , $page);
		$this->assign("page_num" , ceil( $total_num / $this->_limit ));
		$this->display();
	}
	
	public function savepass() {
		if( IS_POST ) {
			if( !$this->_post['org_pass'] ) $this->error("原始密码不能为空");
			if( !$this->_post['new_pass'] ) $this->error("新密码不能为空");
			if( $this->_member_info['passwd'] != md5( $this->_post['org_pass'] ) ) $this->error("原始密码错误");
			M('logistics_supply')->save(array("id" => $this->_member_info['id'] , 'passwd' => md5( $this->_post['new_pass'] )));
			$this->_member_info['passwd'] = md5( $this->_post['new_pass'] );
			session( "login_info" , $this->_member_info );
			$this->success("密码修改成功");
		} else {
			$this->display();
		}
	}
	
	public function forward() {
		if( IS_AJAX ) {
			if( !$this->_post['money'] ) $this->error("提现金额不能为空");
			$info = M("logistics_supply")->where(array("id" => $this->_member_info['id']))->find();
			if( $info['money'] > $this->_post['money'] ) {
				$this->error("提现金额不能大于" . int($info['money']));
			}
			$this->_post['create_time'] = time();
			$this->_post['logistics_supply_id'] = $this->_member_info['id'];
			M('logistics_supply_drawing')->add($this->_post);
			$info['money'] = $info['money'] - $this->_post['money'];
			$info['freeze_money'] = $info['freeze_money'] + $this->_post['money'];
			M("logistics_supply")->save( $info );
			session("login_info" , $info);
			$this->success("提现成功");
		}
	}
}
